" author: iColin <colinvivy@gmail.com>
" link: http://icolin.org
" since: 2011-01-15
"
" [!] 2011-03-18 清理vimrc 修改html中i,em标签里面的文本不使用斜体
" [!] 2011-03-18 更改主题文件名为iColin
"
if exists("mingcheng")
    finish
endif
let g:mingcheng = 1

" 获取当前目录
func! GetPWD()
    return substitute(getcwd(), "", "", "g")
endf

" 全选
func! SelectAll()
    let s:current = line('.')
    exe "norm gg" . (&slm == "" ? "VG" : "gH\<C-O>G")
endfunc

" =========
" 环境配置
" =========
" 保留历史记录
set history=400

" 行控制
set linebreak
set nocompatible
set textwidth=170
set wrap

" 标签页
set tabpagemax=9
set showtabline=2

" 控制台响铃
set noerrorbells
set novisualbell
set t_vb= "close visual bell

" 行号和标尺
set number
set ruler
set rulerformat=%15(%c%V\ %p%%%)

" 命令行于状态行
set ch=1
set stl=\ [File]\ %F%m%r%h%y[%{&fileformat},%{&fileencoding}]\ %w\ \ [PWD]\ %r%{GetPWD()}%h\ %=\ [Line]%l/%L\ %=\[%P]
set ls=2 " 始终显示状态行

" 搜索
set hlsearch
set noincsearch

" 制表符
set tabstop=4
set expandtab
set smarttab
set shiftwidth=4
set softtabstop=4

" 状态栏显示目前所执行的指令
set showcmd 

" 缩进
set autoindent
set smartindent

" 自动重新读入
set autoread

" 插入模式下使用 <BS>、<Del> <C-W> <C-U>
set backspace=indent,eol,start

" 设定在任何模式下鼠标都可用
set mouse=a

" 备份和缓存
set nobackup
set noswapfile

" 自动完成
set complete=.,w,b,k,t,i
set completeopt=longest,menu

" 代码折叠
set foldmethod=indent
set foldlevel=100

" =====================
" 多语言环境
"    默认为 UTF-8 编码
" =====================
if has("multi_byte")
    " vim的默认编码，如果打开的文件编码和此编码不一致，vim会将编码切换此此属性配置编码再显示
    set encoding=utf-8
    " English messages only
    "language messages zh_CN.utf-8

    if has('win32')
        language english
        let &termencoding=&encoding
    endif

    " Vim在读取文件的时候，会根据这里的编码自动检测
    " 如果检测失败则尝试下一个，直到检测成功为止。如果一直没有转换成功，则此值会为空
    set fencs=utf-8,gbk,chinese,latin1

    " fileencoding(fenc):
    " 这个项目配置的是新建文件和保存文件时文件的编码，如果它的值与enc不一样
    " 那么保存的时候Vim会自动把文件内容由enc的编码转换为fenc配置编码再保存。
    " 而读取文件的时候，该选项的值会自动同步为fencs配置的有效编码。

    set formatoptions+=mM
    set nobomb " 不使用 Unicode 签名

    if v:lang =~? '^\(zh\)\|\(ja\)\|\(ko\)'
        set ambiwidth=double
    endif
else
    echoerr "Sorry, this version of (g)vim was not compiled with +multi_byte"
endif


" =========
" AutoCmd
" =========
if has("autocmd")
    filetype plugin indent on

    " 括号自动补全
    func! AutoClose()
        :inoremap ( ()<ESC>i
        :inoremap " ""<ESC>i
        :inoremap ' ''<ESC>i
        :inoremap { {}<ESC>i
        :inoremap [ []<ESC>i
        :inoremap < <><ESC>i
        :inoremap ) <c-r>=ClosePair(')')<CR>
        :inoremap } <c-r>=ClosePair('}')<CR>
        :inoremap ] <c-r>=ClosePair(']')<CR>
    endf

    func! ClosePair(char)
        if getline('.')[col('.') - 1] == a:char
            return "\<Right>"
        else
            return a:char
        endif
    endf

    augroup vimrcEx
        au!
        autocmd FileType text setlocal textwidth=80
        autocmd BufReadPost *
                    \ if line("'\"") > 0 && line("'\"") <= line("$") |
                    \   exe "normal g`\"" |
                    \ endif
    augroup END

    "auto close quotation marks for PHP, Javascript, etc, file
    au FileType php,c,python,javascript,html,css exe AutoClose()

    " Auto Check Syntax
    au BufWritePost,FileWritePost *.js,*.php call CheckSyntax(1)

    " JavaScript 语法高亮
    au FileType html,javascript let g:javascript_enable_domhtmlcss = 1

    " 给 Javascript 文件添加 Dict
    if has('gui_macvim') || has('unix')
        au FileType javascript setlocal dict+=~/.vim/dict/javascript.dict
    else 
        au FileType javascript setlocal dict+=$VIM/vimfiles/dict/javascript.dict
    endif

    " 格式化 JavaScript 文件
    au FileType javascript map <f10> :call g:Jsbeautify()<cr>
    command Jsb :call g:Jsbeautify()
    au FileType javascript set omnifunc=javascriptcomplete#CompleteJS

    " 给 CSS 文件添加 Dict
    if has('gui_macvim') || has('unix')
        au FileType css setlocal dict+=~/.vim/dict/css.dict
    else
        au FileType css setlocal dict+=$VIM/vimfiles/dict/css.dict
    endif

    " 增加 ActionScript 语法支持
    au BufNewFile,BufRead *.as setf actionscript 

    " 自动最大化窗口
    if has('gui_running')
        if has("win32")
            au GUIEnter * simalt ~x
        endif
    endif
endif

" =========
" 图形界面
" =========

if has('gui_running')
    " 不显示菜单和工具栏
    set guioptions-=m
    set guioptions-=T

    " 高亮光标所在的行
    set cursorline

    " 编辑器配色
    colorscheme iColin

    if has("win32")
        " Windows 兼容配置
        source $VIMRUNTIME/mswin.vim

        " f11 最大化
        map <f11> :call libcallnr('fullscreen.dll', 'ToggleFullScreen', 0)<cr>

        " 字体配置
        exec 'set guifont='.iconv('Yahei_Consolas_Hybrid', &enc, 'gbk').':h12:cANSI'
        exec 'set guifontwide='.iconv('微软雅黑', &enc, 'gbk').':h12'
    endif

    if has("unix") && !has('gui_macvim')
        set guifont=Courier\ 10\ Pitch\ 11
        set guifontwide=YaHei\ Consolas\ Hybrid\ 11
    endif
endif

" =========
" 快捷键
" =========

" 标签相关的快捷键
nmap <C-t> :tabnew<cr>
nmap <C-p> :tabprevious<cr>
nmap <C-n> :tabnext<cr>
nmap <C-k> :tabclose<cr>
nmap <C-Tab> :tabnext<cr> 
"for i in range(1, &tabpagemax)
"    exec 'nmap <A-'.i.'> '.i.'gt'
"endfor

" 插件快捷键
nmap <C-c><C-d> :NERDTree<cr>
nmap <C-e> :BufExplorer<cr>

" 设置滚动行数
set scroll=20

" 新建 XHTML 、PHP、Javascript 文件的快捷键
nmap <C-c><C-h> :NewQuickTemplateTab html<cr>
nmap <C-c><C-q> :NewQuickTemplateTab html-qqshop<cr>
nmap <C-c><C-x> :NewQuickTemplateTab xhtml<cr>
nmap <C-c><C-p> :NewQuickTemplateTab php<cr>
nmap <C-c><C-j> :NewQuickTemplateTab javascript<cr>
nmap <C-c><C-c> :NewQuickTemplateTab css<cr>

" Visual 中用 tab 和 shif-tab 做缩排
vmap <Tab>   :><CR>gv
vmap <S-Tab> :<<CR>gv

" =========
" 插件
" =========
" Javascript in CheckSyntax
let g:checksyntax_cmd_javascript  = 'jsl -conf '.shellescape($VIM . '\vimfiles\plugin\jsl.conf')
let g:checksyntax_cmd_javascript .= ' -nofilelisting -nocontext -nosummary -nologo -process'

" VIM HTML 插件
let g:no_html_toolbar = 'yes'

" on Windows, default charset is gbk
if has("win32")
    let g:fontsize#encoding = "gbk"
endif

" 保证语法高亮
syntax on

" 干掉那讨厌的F1(本本键盘F1和ESC太近了)
imap <F1> <Esc>
nmap <F1> <Esc>

" F2自动补全css的A标签
nmap <F2> yyA:link,<ESC>pA:visited {color:#014ccc;}<ESC>pA:hover {color:#ff4e00;}<ESC><cr>
imap <F2> yyA:link,<ESC>pA:visited {color:#014ccc;}<ESC>pA:hover {color:#ff4e00;}<ESC><cr>

"  auto clear float
nmap <Leader>cf <ESC>A:after {content:'';display:block;clear:both;height:0;visibility:hidden;}<cr>

" 一键执行当前html文件
func! RunThisFile()
    silent exec '! D:\tool\bat\chrome.lnk '.expand("%:p")
endf
map <F6> :call RunThisFile()<cr>

" F4 FTP同步
func! FtpUploadFile()
    exec '! D:\tool\develop\EasyPHP\php\php.exe -f D:\FtpSync.php -- -f '.expand("%:p")
endf
nmap <F4> :call FtpUploadFile()<cr>

" F3 进行PHP语法检查
function! PhpDebug()
    execute '! D:\tool\develop\EasyPHP\php\php.exe -l '.expand("%:p")
endfunction
nmap <F3> :call PhpDebug()<cr>


map <F12> :tabnew $home\_vimrc<cr>
map <C-w> :q!<cr>

" set lineHeight
" set linespace =2

" Automatic updating vimrc
if has("autocmd")
autocmd bufwritepost _vimrc source $MYVIMRC
endif 

" 注释宏
nmap <Leader>] I// <ESC><cr>
nmap <Leader>[ ^3x<ESC><cr>

" 清除搜索高亮
nmap <Leader>c <ESC>/```<ESC><cr>

" css格式化
func! CssFormat()
    :%s/\n//g
    :%s/}/}\r/g
    :%s/\*\//\*\/\r/g
    :%s/\/\*/\r\/\*/g
    :%s/{\s/{/g
    :%s/;\s/;/g
    :%s/\(\w\){/\1 {/g
endfunction
nmap <F7> :call CssFormat()<cr>

" shortcut to munual
func! GetManual()
    exec '! D:\manual\php.chm'
endf
nmap <silent> <Leader>php :call GetManual() <cr>
